<!--  -->
<template>
    <el-form label-width="100">
        <el-form-item label="SKU名称">
            <el-input placeholder="SKU名称" v-model="skuParams.skuName"></el-input>
        </el-form-item>
        <el-form-item label="价格(元)">
            <el-input placeholder="价格(元)" v-model="skuParams.price"></el-input>
        </el-form-item>
        <el-form-item label="重量(g)">
            <el-input placeholder="重量(g)" type="number" v-model="skuParams.weight"></el-input>
        </el-form-item>
        <el-form-item label="SKU描述">
            <el-input placeholder="SKU描述" type="textarea" v-model="skuParams.skuDesc"></el-input>
        </el-form-item>
        <el-form-item label="平台属性">
            <el-form :inline="true">
                <el-form-item v-for="item in attrArr" :key="item.id" :label="item.attrName">
                    <el-select v-model="item.attrIdAndValueId">
                        <el-option v-for="attrValue in item.attrValueList" :key="attrValue.id" :label="attrValue.valueName"
                            :value="`${item.id}:${attrValue.id}`"></el-option>
                    </el-select>
                </el-form-item>
            </el-form>
        </el-form-item>
        <el-form-item label="销售属性">
            <el-form :inline="true">
                <el-form-item  v-for="(item ) in saleArr" :key="item.id" :label="item.saleAttrName">
                    <el-select v-model="item.saleIdAndValueId">
                        <el-option v-for="(items,index)  in item.spuSaleAttrValueList" :key="index" :label="items.saleAttrValueName" :value="`${item.id}:${items.id}`"></el-option>
                    </el-select>
                </el-form-item>
            </el-form>

        </el-form-item>
        <el-form-item label="图片名称">
            <el-table border :data="imageArr" ref="table">
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column label="图片" property="imgUrl">
                    <template #="{row}">
                        <img :src="row.imgUrl" alt="" class="w-14 h-14">
                    </template>
                </el-table-column>
                <el-table-column label="名称" property="imgName"></el-table-column>
                <el-table-column label="操作" align="center">
                    <template #="{row}">
                        <el-button type="primary" @click="handle(row)" size="default">设为默认</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" size="default" @click="save">保存</el-button>
            <el-button type="primary" size="default" @click="cancel">取消</el-button>
        </el-form-item>
    </el-form>
</template>

<script setup lang="ts">
import { ElMessage } from 'element-plus'
//引入请求的api
import { reqAttr } from '../../../api/product/attr/index'

import { reqSpuImageList, reqSpuHasSaleAttr ,reqAddSku } from '../../../api/product/spu/index'
import { ref ,reactive} from 'vue'
//品台属性
let attrArr = ref<any>()
//销售属性
let saleArr = ref<any>()
//相关图片
let imageArr = ref<any>()


// 表格table的实例
const table=ref<any>()
// 收集SKU的参数
let skuParams=reactive<any>(
    {
    //父组件传递过来的数据
    "category3Id": "",//三级分类的ID
    "spuId": "",//已有的SPU的ID
    "tmId": "",//SPU品牌的ID
    //v-model收集
    "skuName": "",//sku名字
    "price": "",//sku价格
    "weight": "",//sku重量
    "skuDesc": "",//sku的描述

    "skuAttrValueList": [//平台属性的收集
    ],
    "skuSaleAttrValueList": [//销售属性的收集
    ],
    "skuDefaultImg": "",//sku图片地址
}
)



//当前子组件的方法的暴露
const initSkuData = async (c1Id: any, c2Id: any, row: any) => {
    console.log(row);
    
    //收集数据
    skuParams.category3Id=row.category3Id
    skuParams.spuId=row.id
    skuParams.tmId=row.tmId


    //获取平台属性
    const result: any = await reqAttr(c1Id, c2Id, row.category3Id)
    //获取对应的销售属性
    const result1: any = await reqSpuHasSaleAttr(row.id)
    //获取对应的图片
    const result2: any = await reqSpuImageList(row.id)

    // 平台属性
    attrArr.value = result.data
    //销售属性
    saleArr.value = result1.data
    // 图片的数据
    imageArr.value = result2.data
}

//设置默认图片的设置回调
const handle=(row:any)=>{
    //点击的时候,全部的图片的复选框不勾选
    imageArr.value.forEach((item:any)=>{
        table.value.toggleRowSelection(item,false)
    })
    table.value.toggleRowSelection(row,true)

    skuParams.skuDefaultImg=row.imgUrl

    
}

//保存数据按钮的回调
const save=async()=>{
    //整理参数
    //平台参数
    skuParams.skuAttrValueList=attrArr.value.reduce((prev:any,next:any)=>{
        if (next.attrIdAndValueId) {
            let [attrId,valueId]= next.attrIdAndValueId.split(":")
            prev.push({
                attrId,valueId
            })
        }
        return prev
    },[])
    // 销售属性
    skuParams.skuSaleAttrValueList=saleArr.value.reduce((prev:any,next:any)=>{
        if (next.saleIdAndValueId) {
            let [saleAttrId,saleAttrValueId]= next.saleIdAndValueId.split(":")
            prev.push({
                saleAttrId,
                saleAttrValueId
            })
        }
        return prev
    },[])
    //发请求
console.log(skuParams);
const result: any = await reqAddSku(skuParams)
console.log(result);

if (result.code==200) {
    ElMessage({
    showClose: true,
    message: '添加sku成功',
    type: 'success',
  })   
}




}

//取消按钮的回调
const cancel = () => {
    $emit('changeScene', { flag: 0, params: '' });
}
// 把子组件的方法暴露出去
defineExpose({ initSkuData })

//
let $emit = defineEmits(["changeScene"])
</script>

<style scoped>
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>